package com.bw.module_login.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Intent
import android.view.animation.LinearInterpolator
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.login.vm.LoginViewModel
import com.blankj.utilcode.constant.RegexConstants
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.entity.UserEntity
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_login.R
import com.bw.module_login.databinding.ActivityRegisterBinding
import kotlin.random.Random

@Route(path = RouterPath.PATH_REGISTER_PAGE)
class RegisterActivity : BaseMvvmActivity<ActivityRegisterBinding>() {

    private val vm: LoginViewModel by viewModels()
    private var mValue:Int=0
    private lateinit var mValueAnimator:ValueAnimator

    override fun initViews() {
        super.initViews()

        mDataBinding.includeRegisterLayout.tvGoLogin.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_LOGIN_PAGE).navigation()
        }

        //点击获取验证码
        mDataBinding.includeRegisterLayout.tvVerifyCode.setOnClickListener {
            if (mValue==0){
                getPhoneVerifyCode()
            }
        }
    }

    override fun loadData() {
        super.loadData()
        vm.mSuccessLiveData.observe(this){
            val user=it as UserEntity
            showToast("注册成功")
//            ARouter.getInstance()
//                .build(RouterPath.PATH_MAIN_PAGE)
//                .withString("username",user.username)
//                .withString("password",user.password)
//                .navigation()
            startActivity(Intent(this@RegisterActivity,LoginActivity::class.java)
                .putExtra("username",user.username)
                .putExtra("password",user.password))
        }
    }

    //15秒倒计时 获取验证码
    private fun getPhoneVerifyCode(){

        //填入验证码
        val verifyCode= 100000+Random.nextInt(0,899999)
        mDataBinding.includeRegisterLayout.edtVerifyCode.setText("${verifyCode}")

        mValueAnimator= ValueAnimator.ofInt(15,0)
        mValueAnimator.duration=15000
        mValueAnimator.interpolator= LinearInterpolator()//线性匀速差值器

        //监听动画值的变化
        mValueAnimator.addUpdateListener {
            var count=it.animatedValue as Int
            if (count!=mValue){
                mValue=count
                mDataBinding.includeRegisterLayout.tvVerifyCode.text="${mValue}秒后重新获取"
            }
        }

        //监听动画执行完成
        mValueAnimator.addListener(object : AnimatorListenerAdapter(){
            override fun onAnimationEnd(animation: Animator) {
                mDataBinding.includeRegisterLayout.tvVerifyCode.setText("获取验证码")
            }
        })

        //启动动画
        mValueAnimator.start()

        //点击注册
        mDataBinding.includeRegisterLayout.tvRegisterBtn.setOnClickListener {

            if (!RegexUtils.isMatch(RegexConstants.REGEX_MOBILE_EXACT,mDataBinding.includeRegisterLayout.edtUserName.text.toString())){
                ToastUtils.showLong("用户名不合法")
                return@setOnClickListener
            }
            if (mDataBinding.includeRegisterLayout.edtVerifyCode.text.toString().isEmpty()){
                ToastUtils.showLong("验证码不能为空")
                return@setOnClickListener
            }
            if (!RegexUtils.isMatch(RegexConstants.REGEX_USERNAME,mDataBinding.includeRegisterLayout.edtPwd.text.toString())){
                ToastUtils.showLong("密码不合法")
                return@setOnClickListener
            }
            if (!mDataBinding.includeRegisterLayout.edtPwd.text.toString().equals(mDataBinding.includeRegisterLayout.edtRePwd.text.toString())){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            vm.registerToServer(mDataBinding.includeRegisterLayout.edtPwd.text.toString(),mDataBinding.includeRegisterLayout.edtUserName.text.toString())
        }

    }

    override fun observeDataChange() {
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mValueAnimator!=null){
            mValueAnimator.cancel()
        }
    }

    override fun getLayoutId(): Int =R.layout.activity_register

}